﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using MSD.WL.Site.Models;
using System.Data.Entity;

namespace MSD.WL.Site.Controllers
{
    [Authorize]
    public class RecordController : Controller
    {
        private EMSContext db = new EMSContext();

        // GET: Record
        public ActionResult Inven()
        {
            return View();
        }
        #region 存货档案
        //请求数据
        [HttpPost]
        public JsonResult getInvenRecords(int limit, int offset)
        {
            string txtNum = Request["txtNum"].Trim();
            string txtName = Request["txtName"].Trim();
            string txtTypeNum = Request["txttypeNum"].Trim();
            var Invens = db.Invens as IQueryable<Invens>;
            if (!String.IsNullOrEmpty(txtNum))
            {
                Invens = Invens.Where(x => x.InvenCode.Contains(txtNum));
            }
            if (!String.IsNullOrEmpty(txtName))
            {
                Invens = Invens.Where(x => x.InvenName.Contains(txtName));
            }
            if (!String.IsNullOrEmpty(txtTypeNum))
            {
                Invens = Invens.Where(x => x.InvenTypeCode.Contains(txtTypeNum));
            }
            var invens = Invens.Select(x => new { ID = x.ID, invenCode = x.InvenCode, invenName = x.InvenName, invenTypeCode = x.InvenTypeCode, specsModel = x.SpecsModel, meaUnit = x.MeaUnit, selPrice = x.SelPrice, purPrice = x.PurPrice, smallStockNum = x.SmallStockNum, bigStockNum = x.BigStockNum }).ToList();
            var total = invens.Count();
            var rows = invens.OrderBy(x => x.ID).Skip(offset).Take(limit).ToList();
            return Json(new { total = total, rows = rows });
        }
        //添加存货
        [HttpPost]
        public JsonResult Inven_add(string addArray)
        {
            System.Web.Script.Serialization.JavaScriptSerializer js = new System.Web.Script.Serialization.JavaScriptSerializer();
            var list = js.Deserialize<List<Invens>>(addArray);
            if (list != null)
            {
                foreach (var item in list)
                {
                    Invens Inven = new Invens() { InvenCode = item.InvenCode, InvenName = item.InvenName, InvenTypeCode = item.InvenTypeCode, SpecsModel = item.SpecsModel, MeaUnit = item.MeaUnit, SelPrice = item.SelPrice, PurPrice = item.PurPrice, SmallStockNum = Convert.ToInt32(item.SmallStockNum), BigStockNum = Convert.ToInt32(item.BigStockNum) };
                    db.Entry(Inven).State = EntityState.Added;
                }
                db.SaveChanges();
                string msg = "isok";
                return Json(msg);
            }
            else
            {
                return null;
            }
        }
        //修改存货
        [HttpPost]
        public JsonResult Inven_edit(string addArray,int id)
        {
            System.Web.Script.Serialization.JavaScriptSerializer js = new System.Web.Script.Serialization.JavaScriptSerializer();
            var list = js.Deserialize<List<Invens>>(addArray);
            if (list != null)
            {
                foreach (var item in list)
                {
                    Invens Inven = new Invens() { ID = id, InvenCode = item.InvenCode, InvenName = item.InvenName, InvenTypeCode = item.InvenTypeCode, SpecsModel = item.SpecsModel, MeaUnit = item.MeaUnit, SelPrice = item.SelPrice, PurPrice = item.PurPrice, SmallStockNum = Convert.ToInt32(item.SmallStockNum), BigStockNum = Convert.ToInt32(item.BigStockNum) };
                    db.Entry(Inven).State = EntityState.Modified;
                }
                db.SaveChanges();
                string msg = "isok";
                return Json(msg);
            }
            else
            {
                return null;
            }
        }
        //删除存货
        [HttpPost]
        public JsonResult Inven_delete(int[] idArray)
        {
            for (int i = 0; i < idArray.Length; i++)
            {
                int id = idArray[i];
                Invens Inven = db.Invens.FirstOrDefault(u => u.ID == id);
                if (Inven != null)
                {
                    db.Invens.Remove(Inven);
                }
            }
            db.SaveChanges();
            string msg = "isok";
            return Json(msg);
        }
        #endregion

        #region 客户信息
        public ActionResult Customer()
        {
            return View();
        } 
        
        //请求数据
        [HttpPost]
        public JsonResult getCustomerRecords(int offset,int limit)
        {
            var Customers = db.Customer as IQueryable<Customer>;
            string txtCustomerCode = Request["txtCustomerCode"].Trim();
            string txtCustomerName = Request["txtCustomerName"].Trim();
            string txtEmployeeCode = Request["txtEmployeeCode"].Trim();
            string txtStateCode = Request["txtStateCode"].Trim();
            string txtTradeCode = Request["txtTradeCode"].Trim();
            if (!String.IsNullOrEmpty(txtCustomerCode))
            {
                Customers = Customers.Where(x => x.CustomerCode.Contains(txtCustomerCode));
            }
            if (!String.IsNullOrEmpty(txtCustomerName))
            {
                Customers = Customers.Where(x => x.CustomerName.Contains(txtCustomerName));
            }
            if (!String.IsNullOrEmpty(txtEmployeeCode))
            {
                Customers = Customers.Where(x => x.EmployeeCode.Contains(txtEmployeeCode));
            }
            if (!String.IsNullOrEmpty(txtStateCode))
            {
                Customers = Customers.Where(x => x.StateCode.Contains(txtStateCode));
            }
            if (!String.IsNullOrEmpty(txtTradeCode))
            {
                Customers = Customers.Where(x => x.TradeCode.Contains(txtTradeCode));
            }
            var customers = Customers.Select(x => new { ID = x.ID, CustomerCode = x.CustomerCode, CustomerName = x.CustomerName, EmployeeCode = x.EmployeeCode, AtrMan = x.AtrMan, TelephoneCode = x.TelephoneCode, FaxCode = x.FaxCode, PostCode = x.PostCode, Email = x.Email, Linkman = x.Linkman, Url = x.Url, Address = x.Address, GradeCode = x.GradeCode, StateCode = x.StateCode, CreditCode = x.CreditCode, TradeCode = x.TradeCode }).ToList();
            var total = customers.Count();
            var rows = customers.OrderBy(x => x.ID).Skip(offset).Take(limit).ToList();
            return Json(new { total = total, rows = rows });
        }
        #endregion

        #region 员工信息
        public ActionResult Employees()
        {
            return View();
        }
        //获取数据
        [HttpPost]
        public JsonResult getEmployeeRecords(int offset,int limit)
        {
            var Employees = db.Employee as IQueryable<Employee>;

            string txtEmployeeCode = Request["txtEmployeeCode"].Trim();
            string txtEmployeeName = Request["txtEmployeeName"].Trim();
            string txtDepartmentCode = Request["txtDepartmentCode"].Trim();
            string txtJoinDate = Request["txtJoinDate"].Trim();
            if (!String.IsNullOrEmpty(txtEmployeeCode))
            {
                Employees = Employees.Where(x => x.EmployeeCode.Contains(txtEmployeeCode));
            }
            if (!String.IsNullOrEmpty(txtEmployeeName))
            {
                Employees = Employees.Where(x => x.EmployeeName.Contains(txtEmployeeName));
            }
            if (!String.IsNullOrEmpty(txtDepartmentCode))
            {
                Employees = Employees.Where(x => x.DepartmentCode.Contains(txtDepartmentCode));
            }
            if (!String.IsNullOrEmpty(txtJoinDate))
            {
                DateTime datetime = Convert.ToDateTime(txtJoinDate);
                Employees = Employees.Where(x => x.JoinDate==datetime);
            }
            var employes = Employees.Select(x => new { ID = x.ID,EmployeeCode=x.EmployeeCode,EmployeeName=x.EmployeeName,DepartmentCode=x.DepartmentCode,Age=x.Age,Sex=x.Sex,EduLevel=x.EduLevel,Job=x.Job,JoinDate=x.JoinDate, TelephoneCode = x.TelephoneCode,Remark=x.Remark }).ToList();
            var total = employes.Count();
            var rows = employes.OrderBy(x=>x.ID).Skip(offset).Take(limit).ToList();
            return Json(new { total = total, rows = rows });
        }
        /// <summary>
        /// 根据前台提交的数据进行相应添加或者修改操作
        /// </summary>
        /// <param name="optionArray">json数据</param>
        /// <param name="id">主键值</param>
        /// <param name="option">要进行的操作</param>
        /// <returns></returns>
        [HttpPost]
        public JsonResult Employee_option(string optionArray, int id,string option)
        {
            System.Web.Script.Serialization.JavaScriptSerializer js = new System.Web.Script.Serialization.JavaScriptSerializer();
            var list = js.Deserialize<List<Employee>>(optionArray);
            if (list != null)
            {
                if (option == "add")
                {
                    foreach (var item in list)
                    {
                        DateTime datetime = Convert.ToDateTime(item.JoinDate);
                        Employee Employee = new Employee { EmployeeCode = item.EmployeeCode, EmployeeName = item.EmployeeName, DepartmentCode = item.DepartmentCode, Age = item.Age, Sex = item.Sex, EduLevel = item.EduLevel, Job = item.Job, JoinDate = datetime, TelephoneCode = item.TelephoneCode, Remark = item.Remark };
                        db.Entry(Employee).State = EntityState.Added;
                    }
                }
                if (option == "edit" || option == "editById")
                {
                    foreach (var item in list)
                    {
                        DateTime datetime = Convert.ToDateTime(item.JoinDate);
                        Employee Employee = new Employee { ID=id,EmployeeCode = item.EmployeeCode, EmployeeName = item.EmployeeName, DepartmentCode = item.DepartmentCode, Age = item.Age, Sex = item.Sex, EduLevel = item.EduLevel, Job = item.Job, JoinDate = datetime, TelephoneCode = item.TelephoneCode, Remark = item.Remark };
                        db.Entry(Employee).State = EntityState.Modified;
                    }
                }
                db.SaveChanges();
                string msg = "isok";
                return Json(msg);
            }
            else
            {
                return null;
            }
        }

        public JsonResult Employee_delete(int[] idArray)
        {
            for (int i = 0; i < idArray.Length; i++)
            {
                int id = idArray[i];
                Employee Employees = db.Employee.FirstOrDefault(u => u.ID == id);
                if (Employees != null)
                {
                    db.Employee.Remove(Employees);
                }
            }
            db.SaveChanges();
            string msg = "isok";
            return Json(msg);
        }
        #endregion
    }
}